<template>
	<view class="content">
		<!-- HTML -->
		<mumu-get-qrcode @success='qrcodeSucess' @error="qrcodeError"></mumu-get-qrcode>
	</view>
</template>

<script>
	import { getUInfo } from '@/util/util.js'
	import mumuGetQrcode from '@/uni_modules/mumu-getQrcode/components/mumu-getQrcode/mumu-getQrcode.vue'
	export default {
		components: { mumuGetQrcode },
		data() {
			return {
				type: '', // 首页传过来的类型 1 优惠券码 2 托运单码
				myCp: {}, // 记录扫描出的优惠券对象
				qr_src_data: '', // 记录扫码的原始数据 
			}
		},

		onLoad(opts) {
			console.log('opts.type = ', opts.type);
			this.type = `${opts.type}`
		},

		mounted() {

			let that = this;
			if (that.type == 1) { // 优惠券码
				uni.setNavigationBarTitle({ title: '扫描优惠券码' })
			} else if (that.type == 2) { // 托运单码
				uni.setNavigationBarTitle({ title: '扫描托运单码' })
			}

			// this.reqCmmOrderStatus('69556_643929171488')
		},

		methods: {

			qrcodeSucess(data) {
				// 保存扫码数据
				// uni.setStorageSync('qr_code_data', data)
				// 通过事件总线发送数据

				// uni.showModal({
				// 	title: '二维码识别成功',
				// 	content: data,
				// 	success: res => {
				// 		uni.navigateBack({})
				// 	}
				// })

				this.qr_src_data = data

				let that = this

				if (that.type == 1) { // 优惠券码
					try {
						that.myCp = JSON.parse(data)
						uni.$emit('onGetQrCode', { qr_code_data: data, type: that.type })
						uni.showModal({
							title: '提示',
							content: `识别成功！面值:¥${that.myCp.money} 有效期至:${that.myCp.end_time_text}`,
							showCancel: false,
							success: res => {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						})
					} catch (error) {
						uni.showModal({
							title: '提示',
							content: `请扫描正确的优惠券码!`,
							showCancel: false,
							success: res => {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						})
					}
				} else if (that.type == 2) { // 托运单码
					try {
						// 解析托运单码数据,获取qr字段的值
						if (data && data.includes('qr=')) {
							let t1 = data.split('qr=')[1]
							if (t1 && (t1.endsWith('"') || t1.endsWith('”') || t1.endsWith('“'))) {
								t1 = t1.slice(0, -1)
							}

							// uni.showModal({ title: '调试', content: `请求的单号= ${t1}` })

							// 根据qr字段的值,获取运单详情
							that.reqCmmOrderStatus(t1)
						} else {
							uni.showModal({
								title: '提示',
								content: `请扫描正确的托运单码!`,
								showCancel: false,
								success: res => {
									if (res.confirm) {
										uni.navigateBack()
									}
								}
							})
						}
					} catch (error) {
						uni.showModal({
							title: '提示',
							content: `请扫描正确的托运单码!`,
							showCancel: false,
							success: res => {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						})
					}

				}

			},

			qrcodeError(err) {
				console.log(err)
				uni.showModal({
					title: '摄像头授权失败',
					content: '摄像头授权失败，请检测当前浏览器是否有摄像头权限。',
					success: res => {
						uni.navigateBack({})
					}
				})
			},

			// 接口:车满满获取运单详情 69556_643929171488
			async reqCmmOrderStatus(query_num) {
				let that = this

				let token = getUInfo().token
				uni.showLoading({ title: '处理中', mask: true, })
				let { code, msg, data } = await that.$cmmOrderStatus({ query_num })
				// console.log(`$cmmOrderStatus...code=${code},msg=${msg},data=`, data)
				if (code == 1) { // 业务请求成功
					let { errno, res } = data
					if (errno == 0) { // 车满满请求成功
						console.log('cmm.res = ', res);
						uni.showModal({ title: '调试', content: `请求成功= ${JSON.stringify(res)}` })
						let { startCity, toCity, od_num } = res
						console.log('发站-到站-运单号 = ', `${startCity}-${toCity}-${od_num}`);

						let order_obj = res
						// 处理货物名称和数量
						if (order_obj.goods && order_obj.goods.length > 0) {
							order_obj['goods_info'] = `${order_obj.goods[0].name}${order_obj.goods[0].num}件`
						} else {
							order_obj['goods_info'] = '未填写货物信息'
						}
						// 增加二维码内容字段
						order_obj['qr_code'] = query_num
						// uni.showModal({ title: '调试', content: `order_obj=${JSON.stringify(order_obj)}` })
						uni.$emit('onGetQrCode', { qr_code_data: JSON.stringify(order_obj), type: that.type })
						uni.showModal({
							title: '提示',
							content: `识别成功！发站:${startCity} 到站:${toCity} 单号:${od_num}`,
							showCancel: false,
							success: res => {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						})
					} else {
						uni.showToast({ icon: 'none', title: `解析托运单信息错误`, duration: 2500 })
					}
				} else {
					uni.showToast({ icon: 'none', title: `${msg}`, duration: 2500 })
				}
			},

		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
</style>